package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.controls.Alert;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.GetPractitionerEvent;
	import net.optix.ip.events.UpdatePractitionerEvent;
	import net.optix.ip.model.ModelLocator;

	public class UpdatePractitionerCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();


		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			
			var myEvent:UpdatePractitionerEvent = UpdatePractitionerEvent( event );
			
			var delegate:UserDelegate = new UserDelegate();     
			delegate.updatePractitioner( this , myEvent.vo );
			
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{
			var title:String	=	"Result of update practitioner ";
			var message:String;
			try
			{
				var practitioner:XML	=	event.result;
				var result:int			=	practitioner.result;
			
				switch(result)
				{
					case -2:
						message	=	"Access Denied.";
						break;
					case 0:
						message	=	"Program error!";
						break;
					case 1:
						message	=	"Successfully update practitioner!";
						var getPractitioner:GetPractitionerEvent	=	new GetPractitionerEvent(model.currentPractitioner.practitionerID);
						com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getPractitioner);
						break;	
				}
			}
			catch(error:Error)
			{
				message	=	"Program error!";
			}
			model.bSubmit	=	true;
			var alert:Alert	=	 
					Alert.show(message,title,Alert.OK, null,null, null,  Alert.OK );
			alert.width		=	360;
			alert.height	=	130;
		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			trace(event.fault);
			model.result	=	"Program error";
		}

	}
}